package org.example.util;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 * Company 源辰信息
 *
 * @author navy
 * @date 2024/7/14
 * Email haijunzhou@hnit.edu.cn
 */
public class PiderUtils {
    public static void main1(String[] args) throws IOException {
        URL url = new URL("https://www.vvic.com/apif/index/goodShops?city=gz");
        URLConnection urlConnection = url.openConnection();
        InputStream inputStream = urlConnection.getInputStream();

        InputStreamReader isr = new InputStreamReader(inputStream);
        StringBuilder sb = new StringBuilder();
        char[] chars = new char[1024];
        int count;
        while((count = isr.read(chars))>-1){
            sb.append(chars, 0, count);
        }
        ObjectMapper mapper = new ObjectMapper();
        HashMap hashMap = mapper.readValue(sb.toString(), HashMap.class);
        Map map = (Map) hashMap.get("data");
        List list = (List) map.get("good_shops");

        list.forEach(System.out::println);


    }

    public static void main(String[] args) throws IOException {
        String addr = "https://www.jiushujie.com/sell?ajax=yw0&page=1&sort=created_at.desc&tag=recommend";
        Document doc = Jsoup.parse(new URL(addr), 5000);
        Elements elements = doc.select("div.book_pic>a");
        List<Map<String,Object>> books = new ArrayList<>();
        for (Element el : elements) {
            Map<String,Object> book = new HashMap<>();
            String href = el.attr("href");
            System.out.println("href = " + href);
            try{
                Document doc1  = Jsoup.parse(new URL(href), 5000);
                book.put("name", doc1.select("div.info>h2").text());

                String detail = doc1.select("div.info>div.detail").text();
                String[] split = detail.split("[\\s/]+");
                if(split.length == 7){
                    book.put("press", split[0]);
                    book.put("author", split[1]);
                    book.put("isbn", split[2]);
                    book.put("pric1", split[3]);
                    book.put("pric2", split[4]);
                    book.put("zj", split[5]);
                    book.put("catetory", split[6]);
                }
                book.put("img",doc1.select("div.book_pic>img").attr("src"));
                book.put("intro", doc1.select("div.book_introduction_container").html());
                books.add(book);
            }catch (Exception e){
                System.out.println("读取书籍信息失败: " + href);
            }
        }
        books.forEach(System.out::println);
    }

}
